<?php
/**
 * Currencies Controller
 *
 * PHP version 5
 *
 * @category Controller
 * @package  Croogo
 * @version  1.0
 * @author   Tran Van Dung <dungtv@ic-t.info>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.croogo.org
 */
class CurrenciesController extends AppController {
/**
 * Controller name
 *
 * @var string
 * @access public
 */
    public $name = 'Currencies';
/**
 * Models used by the Controller
 *
 * @var array
 * @access public
 */
    public $uses = array('Currency');
/**
 * Helpers used by the Controller
 *
 * @var array
 * @access public
 */
    public $helpers = array('Text');

    protected $paginate = array(
        'limit' => 10,
        );

/**
 * Before executing controller actions
 *
 * @return void
 * @access public
 */
    public function beforeFilter() {
        parent::beforeFilter();

        if ($this->action == 'add' || $this->action == 'edit' || $this->action == 'admin_add' || $this->action == 'admin_edit') {
            $this->Security->validatePost = false;
        }
    }

/**
 * Admin index
 *
 * @return void
 * @access public
 */
    public function admin_index() {
        $this->set('title_for_layout', __('CURRENCY LIST'));

        $this->Currency->recursive = 0;
        $this->paginate['Currency']['order'] = 'Currency.name ASC';
        $this->set('currencies', $this->paginate());
    }

/**
 * Admin add
 *
 * @return void
 * @access public
 */
    public function admin_add() {
        $this->set('title_for_layout', __('CURRENCY DETAIL'));

        if (!empty($this->request->data)) {
            
            $this->Currency->create();
            if ($this->Currency->save($this->request->data)) {
                //$this->Session->setFlash(__('The Currency has been saved'), 'default', array('class' => 'success'));

                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Currency could not be saved. Please, try again.'), 'default', array('class' => 'error'));
            } 
        } 
    }

/**
 * Admin edit
 *
 * @param int $id 
 * @return void
 * @access public
 */
    public function admin_edit($id = null) {
        $this->set('title_for_layout', __('CURRENCY DETAIL'));

        if (!$id && empty($this->request->data)) {
            $this->Session->setFlash(__('Invalid Currency'), 'default', array('class' => 'error'));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->request->data)) {
            if ($this->Currency->save($this->request->data)) {
                //$this->Session->setFlash(__('The Currency has been saved'), 'default', array('class' => 'success'));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Currency could not be saved. Please, try again.'), 'default', array('class' => 'error'));
            }
        }
        if (empty($this->request->data)) {
            $this->request->data = $this->Currency->read(null, $id);
        }
    }

/**
 * Admin delete
 *
 * @param int $id 
 * @return void
 * @access public
 */
    public function admin_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for Currency'), 'default', array('class' => 'error'));
            $this->redirect(array('action'=>'index'));
        }

        $currency = $this->Currency->find('first', array(
            'conditions' => array(
                'id' => $id,
            ),
        ));
        if (isset($currency['Currency'])) {
            if($currency['Currency']['lock']<=0) {
                if ($this->Currency->delete($id)) {
                    //$this->Session->setFlash(__('Currency deleted'), 'default', array('class' => 'success'));
                    $this->redirect(array('action'=>'index'));
                } else {
                    //
                }
            } else {
                $this->Session->setFlash(__('Currency was locked'), 'default', array('class' => 'success'));
                $this->redirect(array('action'=>'index'));
            }
        } else {
            $this->Session->setFlash(__('Invalid id for Currency'), 'default', array('class' => 'error'));
            $this->redirect(array('action'=>'index'));
        }
    }
    public function admin_checkunique($id = '') {

        $this->layout = 'ajax';
        $success = false;
        if ($id != '' || $id != 'undefined') {
            $banks = $this->Currency->find('first', array('fields' => array('Currency.name'), 'conditions' => array('Currency.name' => $id,)));
            if ($banks) {
                $success = true;
                $success = $this->Currency->encodeData(array('success' => $success, 'isExsist' => '1'), array('json' => true, 'trim' => false));
            }else{
                $success = true;
                $success = $this->Currency->encodeData(array('success' => $success, 'isExsist' => '0'), array('json' => true, 'trim' => false));
            }
        } else {
            $success = $this->Currency->encodeData(array('success' => $success, 'isExsist' => '0'), array('json' => true, 'trim' => false));
        }
        $this->set('success', $success);
    }

}
?>